<%@ page import="br.com.rosena.control.ControllerRosena" %>
<%@ page import="br.com.rosena.model.bean.persistente.Classe" %>
<%@ page import="br.com.rosena.model.bean.persistente.Persistente" %>
<%@ page import="java.util.Vector" %>
<%@ page import="br.com.rosena.util.Util" %>
<%@ page import="java.sql.*" %>

<%
    String nomeClasseRR = request.getParameter("nomeClasse") != null ? request.getParameter("nomeClasse") : "";
    String propriedadeInversa = request.getParameter("propriedadeInversa") != null ? request.getParameter("propriedadeInversa") : "";
    String id = request.getParameter("id") != null ? request.getParameter("id") : "";
    Persistente objPersistenteRR = new Persistente(nomeClasseRR);
    Classe objClasseRR = objPersistenteRR.getClasse();
    objClasseRR.iniciarQUERY(objClasseRR,objPersistenteRR);
    Vector cabecalho = new Vector();
    Vector linhas = new Vector();
    String consultaPrincipal;
    if(!id.equals("") ){
        if(!propriedadeInversa.equals("")){
            consultaPrincipal= "SELECT * FROM "+objPersistenteRR.getClasse().getNomeTabelaBancoDados()+" WHERE "+propriedadeInversa+" = "+id;
        }else{
            consultaPrincipal= "SELECT * FROM "+objPersistenteRR.getClasse().getNomeTabelaBancoDados();                                     
        }
    }else{
        return;
    }
    ResultSet crsConsultaPrincipal = new ControllerRosena().execultarQueryReturResultSet(consultaPrincipal);

    if( crsConsultaPrincipal.next() ){
        java.sql.ResultSetMetaData rsmd = crsConsultaPrincipal.getMetaData();
        for (int i = 1; i <= rsmd.getColumnCount(); ++i){
            cabecalho.addElement(rsmd.getColumnName(i));
        }do{
            linhas.addElement(proximaLinha(crsConsultaPrincipal,rsmd));
        }
        while (crsConsultaPrincipal.next());
    }
%>
<html>
<head>
<title>TEX</title>

<link rel="stylesheet" href="../css/blue/style.css">

<!-- jQuery -->
<script src="js/jquery.js"></script>
<script src="js/jquery.tablesorter.js"></script>
<script src="js/jquery.tablesorter.widgets.js"></script>

<style type="text/css">
table.tablesorter thead tr.filters input {
    width: 95%;
    -webkit-box-sizing: border-box;
    -moz-box-sizing: border-box;
    box-sizing: border-box;
}

table.tablesorter thead tr.filters, table.tablesorter thead tr.filters td {
    text-align: center;
    background: #fff;
}

table.tablesorter thead tr.filters input.disabled {
    opacity: 0.5;
    filter: alpha( opacity = 50 );
}

/* Blue Theme */
table.tablesorter {
    font: 11px / 18px Arial, Sans-serif;
    background-color: #cdcdcd;
    margin: 10px 0 15px;
    width: 100%;
    text-align: left;
    border-spacing: 0;
}

table.tablesorter,
    table.tablesorter th,
    table.tablesorter td {
    border: #cdcdcd 1px solid;
}

table.tablesorter th {
    background-color: #99bfe6;
    color: #000;
    border-collapse: collapse;
    font: 12px / 18px Arial, Sans-serif;
    padding: 4px;
}

table.tablesorter .header,
    table.tablesorter .tablesorter-header {
/* black double arrow */
    background-image: url(  ); /* white double arrow */
/* background-image: url(); */
/* image */
/* background-image: url(black-bg.gif); */
    background-repeat: no-repeat;
    background-position: center right;
    padding: 4px 20px 4px 4px;
    cursor: pointer;
}

table.tablesorter tbody td {
    color: #3d3d3d;
    padding: 4px;
    background-color: #fff;
    vertical-align: top;
}

table.tablesorter th.headerSortUp,
    table.tablesorter th.tablesorter-headerSortUp {
    background-color: #9fbfdf; /* black asc arrow */
    background-image: url(  ); /* white asc arrow */
/* background-image: url(); */
/* image */
/* background-image: url(black-asc.gif); */
}

table.tablesorter th.headerSortDown,
    table.tablesorter th.tablesorter-headerSortDown {
    background-color: #8cb3d9; /* black desc arrow */
    background-image: url(  ); /* white desc arrow */
/* background-image: url(); */
/* image */
/* background-image: url(black-desc.gif); */
}

/* used to hide a tbody while rebuilding to speed it up */
table.tablesorter .tablesorter-hidden {
    display: none;
}

/* Zebra Widget - row alternating colors */
table.tablesorter tr.odd td {
    background-color: #ebf2fa;
}

table.tablesorter tr.even td {
    background-color: #fff;
}

/* Column Widget - column sort colors */
.tablesorter td.primary,
    .tablesorter tr.odd td.primary {
    background-color: #99b3e6;
}

.tablesorter tr.even td.primary {
    background-color: #c2d1f0;
}

.tablesorter td.secondary,
    .tablesorter tr.odd td.secondary {
    background-color: #c2d1f0;
}

.tablesorter tr.even td.secondary {
    background-color: #d6e0f5;
}

.tablesorter td.tertiary,
    .tablesorter tr.odd td.tertiary {
    background-color: #d6e0f5;
}

.tablesorter tr.even td.tertiary {
    background-color: #ebf0fa;
}

/* hovered row colors */
table.tablesorter tbody tr:hover td,
    table.tablesorter tbody tr.even:hover td {
    background: #d9d9d9;
}

table.tablesorter tbody tr.odd:hover td {
    background: #bfbfbf;
}

/* filter widget */
table.tablesorter input.tablesorter-filter,
    table.tablesorter select.tablesorter-filter {
    width: 95%;
    height: inherit;
    -webkit-box-sizing: border-box;
    -moz-box-sizing: border-box;
    box-sizing: border-box;
}

table.tablesorter tr.tablesorter-filter,
    table.tablesorter tr.tablesorter-filter td {
    text-align: center;
    background: #fff;
}

/* optional disabled input styling */
table.tablesorter input.tablesorter-filter.disabled,
    table.tablesorter select.tablesorter-filter.disabled {
    opacity: 0.5;
    filter: alpha( opacity = 50 );
}
</style>
<script type="text/javascript">
    $(function() {

        // call the tablesorter plugin
        $("table").tablesorter({

        // initialize zebra striping and filter widgets
            widgets: ["zebra", "filter"],

        // headers: { 5: { sorter: false, filter: false } },

            widgetOptions : {

            // css class applied to the table row containing the filters & the inputs within that row
                filter_cssFilter : 'tablesorter-filter',

            // If there are child rows in the table (rows with class name from "cssChildRow" option)
            // and this option is true and a match is found anywhere in the child row, then it will make that row
            // visible; default is false
                filter_childRows : false,

            // Set this option to true to use the filter to find text from the start of the column
            // So typing in "a" will find "albert" but not "frank", both have a's; default is false
                filter_startsWith : false,

            // Set this option to false to make the searches case sensitive
                filter_ignoreCase : true,

            // Delay in milliseconds before the filter widget starts searching; This option prevents searching for
            // every character while typing and should make searching large tables faster.
                filter_searchDelay : 300,

            // See the filter widget advanced demo on how to use these special functions
                filter_functions : {}

            }
            // ,debug: true
        });

    });
</script>
</head>
<body>
<table class="tablesorter" style="cursor:hand" id="pageNav" >
    <thead>
        <tr  id="cabecalho">
            <%for(int i=0;i<cabecalho.size();i++){%>
            <th>
                <%=cabecalho.get(i).toString().toUpperCase()%>
            </th>
        <%}%>
    </tr>
</thead>
<tbody>

<%for(int i=0;i<linhas.size();i++){%>
    <tr onclick="parent.obj<%=nomeClasseRR%> = parent.preencher('<%=nomeClasseRR%>',<%=((Vector)linhas.get(i)).get(0)%>,parent.obj<%=nomeClasseRR%>);">
        <%for(int j=0;j< ((Vector)linhas.get(i)).size(); j++){%>
            <td>
                <%=((Vector)linhas.get(i)).get(j)%>
            </td>
        <%}%>
    </tr>
<%}%>
</tbody>
</table>
 
</body>
</html>

<%!
    private static Vector proximaLinha(ResultSet rs, java.sql.ResultSetMetaData rsmd) throws Exception {
        Vector LinhaAtual = new Vector();
        try {
            for (int i = 1; i <= rsmd.getColumnCount(); ++i)
                switch (rsmd.getColumnType(i)) {
                    case Types.VARCHAR:
                        LinhaAtual.addElement(rs.getString(i) != null && rs.getString(i).length() > 20 ? rs.getString(i).substring(0, 20) : rs.getString(i));
                        break;
                    case Types.FLOAT:
                        LinhaAtual.addElement(br.com.rosena.util.Util.formatarNumero(rs.getDouble(i),"0.00"));
                        break;
                    case Types.NUMERIC:
                        LinhaAtual.addElement(br.com.rosena.util.Util.formatarNumero(rs.getDouble(i),"0.00"));
                        break;
                    case Types.INTEGER:
                        LinhaAtual.addElement(br.com.rosena.util.Util.formatarNumero(rs.getInt(i), "0"));
                        break;
                    case Types.DOUBLE:
                        LinhaAtual.addElement(br.com.rosena.util.Util.formatarNumero(rs.getDouble(i),"0.00"));
                        break;
                    case Types.BOOLEAN:
                        LinhaAtual.addElement(rs.getBoolean(i) ? "sim" : "nao");
                        break;
                    case Types.TIME:
                        LinhaAtual.addElement(br.com.rosena.util.Util.formatarData(new java.sql.Time(rs.getTime(i).getTime()), "HH:mm"));
                        break;
                    case Types.TIMESTAMP:
                        LinhaAtual.addElement(br.com.rosena.util.Util.formatarData(new java.sql.Time(rs.getTime(i).getTime()), "dd/MM/yyyy HH:mm:ss"));
                        break;
                    case Types.DATE:
                        LinhaAtual.addElement(br.com.rosena.util.Util.formatarData(new java.sql.Date(rs.getDate(i).getTime()), "dd/MM/yyyy"));
                        break;
                    case Types.BIT:
                        LinhaAtual.addElement(rs.getBoolean(i) ? "sim" : "nao");
                        break;
                    case Types.CHAR:
                        LinhaAtual.addElement(rs.getString(i));
                }
        }
        catch (SQLException ex) {
            throw new Exception(ex);
        } catch (Exception ex) {
            throw new Exception(ex);
        }
        return LinhaAtual;
    }
%> 